package com.shuwen.gcdj.service.power;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.entity.PowerAuthDept;
import com.shuwen.gcdj.mapper.mysql.PowerAuthDeptMapper;
import com.shuwen.gcdj.service.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@AllArgsConstructor
public class PowerAuthService extends BaseService<PowerAuthDeptMapper, PowerAuthDept> {

    private final PowerAuthDeptMapper powerAuthDeptMapper;

    /**
     * 获取授权部门id
     *
     * @param rolePowerId List<String>
     * @return List<String>
     */
    public List<String> getAuthDeptIdsByRolePowerId(List<String> rolePowerId) {
        if (rolePowerId == null || rolePowerId.isEmpty()) {
            return new ArrayList<>();
        }

        QueryWrapper<PowerAuthDept> q = new QueryWrapper<>();
        q.eq(Constants.FIELD_KEY_IS_DELETE, Constants.IS_DELETE_NORMAL);
        q.in("role_power_id", rolePowerId);
        List<PowerAuthDept> data = powerAuthDeptMapper.selectList(q);
        if (data == null || data.isEmpty()) {
            return new ArrayList<>();
        }

        List<String> resp = new ArrayList<>();
        for (PowerAuthDept powerAuthDept : data) {
            if (powerAuthDept.getAuthDeptId() != null && !powerAuthDept.getAuthDeptId().isEmpty()) {
                resp.add(powerAuthDept.getAuthDeptId());
            }
        }
        return resp;
    }
}
